package com.sas.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sas.entity.Page;
import com.sas.entity.PageBean;
import com.sas.entity.TTuihhmx;
import com.sas.service.TTuihhmxService;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 * 
 * @author HuQiAn
 * @email 
 * @date 2018-05-26 11:56:16
 */
@Controller
@RequestMapping("tuihuanhmx.do")
public class TTuihhmxController {
	@Autowired
	private TTuihhmxService tTuihhmxService;
	 //分页查询  
		@RequestMapping(params="method=ajax")
		public void listFenYes( String pageNo,String pageSize ,String tradedate,String gysid,HttpSession session,HttpServletResponse response) throws IOException {
			 System.out.println("ajax数据"+pageSize+"//"+pageNo+"//"+tradedate);
			Page p = new Page();
			//每页条数		    
			p.setSize(Integer.parseInt(pageSize));
			//调用service总条数
			Integer sum = tTuihhmxService.selectCount();
			//总页数
			Integer pages = sum%p.getSize()!=0?sum/p.getSize()+1:sum/p.getSize();
			
			p.setCount(pages);
			int index_int = 1;
			//显示第几页
			if(pageNo==null||pageNo==""){
				index_int = 1;
			}else{
				index_int = Integer.parseInt(pageNo);
				if(index_int<=0){
					index_int = 1;
				}
				if(index_int > p.getCount()){
					index_int = p.getCount();
				}
			}
			
			p.setIndex(index_int);
	    	TTuihhmx s=new TTuihhmx();
	    	s.setgysid(gysid);
	    	s.setTradedate(tradedate);
			List<TTuihhmx> list =tTuihhmxService.listFenYe(s,p);
			 ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类
			 
			      //将list中的对象转换为Json格式的数组	     
			  PageBean<TTuihhmx> pg=new PageBean<TTuihhmx>();  
			  pg.setTotal(sum);
			  pg.setRows(list);
			  String json = mapper.writeValueAsString(pg);
			        //将json数据返回给客户端
			      response.setContentType("text/html; charset=utf-8");
			       response.getWriter().write(json);  
			       System.out.println("ajax数据"+json);
		}
	//集合查询
    @RequestMapping(params="method=ajaxjihe")
	public void listselect( String pageSize, HttpServletRequest request,HttpSession session,HttpServletResponse response) throws IOException{
	List<TTuihhmx> list=tTuihhmxService.listselect();
	 ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类
	 
	  String json = mapper.writeValueAsString(list);    //将list中的对象转换为Json格式的数组	     
	  String pageSiz = request.getParameter("pageSize");    
	        //将json数据返回给客户端
	      response.setContentType("text/html; charset=utf-8");
	       response.getWriter().write(json);  
	       System.out.println("ajax数据"+json+"//"+pageSiz);
	       System.out.println("a");
	       System.out.println("数据"+pageSize);
	       System.out.println("b");
	//存入session数据
	//session.setAttribute("list", list);
	//返回页面
		//return "yemian";
	}
    //增加
    @RequestMapping(params="method=insert")
	public void insert(HttpServletRequest request,String tradedate, String gysid,String dindanid,String dianhua,String kuaidiid,String baogyf,String yanscm,String shuliang,String tuihuan,String chulijg,HttpServletResponse response,HttpSession session) throws IOException {
	
    	TTuihhmx aa=new TTuihhmx( tradedate,  gysid, dindanid, dianhua, kuaidiid, baogyf, yanscm, shuliang, tuihuan, chulijg);
    	System.out.println("进入增加数据");
	String[] tTuihhmx = request.getParameterValues("tTuihhmx");
	String[] vm = request.getParameterValues("vm.tTuihhmx");
	System.out.println("进入增加数据"+tTuihhmx+"//"+vm+"//"+aa.toString());
	int h=tTuihhmxService.insert(aa);
	ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类
	 
	  String json = mapper.writeValueAsString(h);    //将list中的对象转换为Json格式的数组	       
	        //将json数据返回给客户端
	      response.setContentType("text/html; charset=utf-8");
	       response.getWriter().write(json);  
	//存入session数据
//	session.setAttribute("h", h);
	//返回页面
	//	return "jsp/tuihuanhgl";
	}
    //删除
	@RequestMapping(params="method=delete")
	public void delete(String id,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException {
		 System.out.println("进入删除数据"+id);
		TTuihhmx aa=new TTuihhmx();
		int parseInt = Integer.parseInt(id);		
		aa.setId(parseInt);
		int delete = tTuihhmxService.delete(aa);
		 ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类
		 
		  String json = mapper.writeValueAsString(delete);    //将list中的对象转换为Json格式的数组	     
	   
		        //将json数据返回给客户端
		      response.setContentType("text/html; charset=utf-8");
		       response.getWriter().write(json); 
	}
    //修改
	@RequestMapping(params="method=update")
	public  void update(HttpServletResponse response,int id, String tradedate, String gysid,String dindanid,String dianhua,String kuaidiid,String baogyf,String yanscm,String shuliang,String tuihuan,String chulijg,HttpSession session) throws IOException {
		TTuihhmx aa=new TTuihhmx(id, tradedate,  gysid, dindanid, dianhua, kuaidiid, baogyf, yanscm, shuliang, tuihuan, chulijg);
		System.out.println("进入修改数据"+aa.toString());
		int h=tTuihhmxService.update(aa);
		 ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类
		 
		  String json = mapper.writeValueAsString(h);    //将list中的对象转换为Json格式的数组	     
	   
		        //将json数据返回给客户端
		      response.setContentType("text/html; charset=utf-8");
		       response.getWriter().write(json); 
		//存入session数据
	  //  session.setAttribute("h", h);
	    //返回页面
		//return "jsp/tuihuanhgl";
	}
    //分页查询  
	@RequestMapping(params="method=selectByFenye")
	public String listFenYe(String index,String xx,HttpSession session) {
	    Page p = new Page();
		//每页条数
		p.setSize(20);
		//调用service总条数
		Integer sum = tTuihhmxService.selectCount();
		//总页数
		Integer pages = sum%p.getSize()!=0?sum/p.getSize()+1:sum/p.getSize();
		
		p.setCount(pages);
		int index_int = 1;
		if(index==null||index==""){
			index_int = 1;
		}else{
			index_int = Integer.parseInt(index);
			if(index_int<=0){
				index_int = 1;
			}
			if(index_int > p.getCount()){
				index_int = p.getCount();
			}
		}
		
		p.setIndex(index_int);
    	TTuihhmx s=new TTuihhmx();
		List<TTuihhmx> list =tTuihhmxService.listFenYe(s,p);
		//存入session数据
	    session.setAttribute("list", list);
	    session.setAttribute("page", p);
	    //返回页面
		return "jsp/tuihuanhgl";
	}
    //统计条数
	@RequestMapping(params="tt2")
	public void selectCount() {
		int h=tTuihhmxService.selectCount();
	}
	/*@RequestMapping(params="method=dayinchukudan")
	public void dayinchukudan(HttpServletResponse response) throws Exception{
		List<Chukudan> list = ShangpinService.selectChukudan();
		//
		response.setContentType("application/vnd.ms-excel");
		Date date = new Date();//设置日期
		DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//ʱ���ʽ��
		String d = dFormat.format(date);//截取日期
		String title = d+"出库单";
		//�ļ���
		String fileName = URLEncoder.encode("出库单","utf-8");
		response.setHeader("Content-disposition", "attachment;fileName="+fileName+".xls");
		//输出流
		OutputStream out = response.getOutputStream();
		
		WritableWorkbook wb = Workbook.createWorkbook(out);
		WritableSheet sheet = wb.createSheet("出库单", 0);
		sheet.getSettings().setDefaultColumnWidth(30);//50*256
		sheet.getSettings().setDefaultRowHeight(10);
		
		WritableFont font = new WritableFont(WritableFont.ARIAL,16,WritableFont.NO_BOLD);
		
		WritableCellFormat format = new WritableCellFormat(font);
		
		format.setBorder(Border.ALL, BorderLineStyle.THIN);
		
		format.setAlignment(Alignment.CENTRE);
		
		format.setWrap(true);
		
		WritableFont font1 = new WritableFont(WritableFont.ARIAL,30,WritableFont.BOLD);
		WritableCellFormat format1 = new WritableCellFormat(font1);
		format1.setAlignment(Alignment.CENTRE);
		
		sheet.mergeCells(0, 0, 2, 0);
		Label t  = new Label(0,0,title,format1);
		sheet.addCell(t);
		
	
		Label t1 = new Label(0,1,"供应商名字",format);
		Label t2 = new Label(1,1,"商品名字",format);		
		Label t3 = new Label(2,1,"地址",format);
		Label t4 = new Label(3,1,"备注",format);
		sheet.addCell(t1);
		sheet.addCell(t2);
		sheet.addCell(t3);
		sheet.addCell(t4);				
		int i = 0;		
		for (Chukudan Shangpin : list) {			
			Label c1 = new Label(0,i+2,Shangpin.getGysname()+"",format);
			Label c2 = new Label(1,i+2,Shangpin.getSpname()+"",format);
			Label c3 = new Label(2,i+2,Shangpin.getUrl()+"",format);
			Label c4 = new Label(3,i+2,Shangpin.getSpsl()+"",format);	
			sheet.addCell(c1);
			sheet.addCell(c2);
			sheet.addCell(c3);
			sheet.addCell(c4);
			i++;
		}	
		wb.write();
		wb.close();
		out.flush();
		out.close();
	} */
}
